package cn.jdy.plugins.wap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.platform.entity.ConfValue;
import cn.jdy.platform.entity.News;
import cn.jdy.platform.service.ConfService;
import cn.jdy.platform.service.NewsService;

/**
 * @author YangYi
 * @title 新闻控制类
 * @date 2014-03-12
 */
@Controller
@RequestMapping("/news")
public class NewsCtrl extends BaseCtrl {
	@Autowired
	private NewsService newsService;
	@Autowired
	private ConfService confService;

	/**
	 * 查询新闻列表
	 * 
	 * @param news
	 * @return
	 */
	@RequestMapping("/{typeid}.html")
	public ModelAndView query(News news, @PathVariable Long typeid) {
		ModelAndView mv = new ModelAndView(super.t + "/news/newsList.jsp");
		try {
			news.setStatus("1");
			news.setColumnId(typeid);
			ConfValue cv = confService.find(typeid); // 获取分类的父id,查询同级分类
			news.setCv(cv);
			if (0 == cv.getPid()) {
				mv.addObject("newsClass", null);// 根据大分类 查询小分类 列表
			} else {
				List<ConfValue> plist = confService.queryByType(
						GlobalConstant.TYPE_NEWS, cv.getPid());// 新闻分类
				mv.addObject("newsClass", plist);// 根据大分类 查询小分类 列表
				// ConfValue pcv = confService.find(cv.getPid()); // 查询父分类名称
				// mv.addObject("typeName", pcv.getValue0());// 父分类的名称 用于
				// 页面导航路径展示
			}
			// 设置分页信息
			if (news.getiDisplayLength() == 0)
				news.setiDisplayLength(5); // 默认长度5
			if (news.getPageNo() <= 0)
				news.setPageNo(1); // 默认页数1
			news.setiDisplayStart((news.getPageNo() - 1)
					* news.getiDisplayLength()); // 显示起始索引
			mv.addObject("typeId", typeid);
			mv.addObject("dataList", newsService.query(news));
			// mv.addObject("newsClass", newsClass());
			mv.addObject("news", news);
			mv.addObject("pageNo", news.getPageNo());
			mv.addObject("size", news.getiDisplayLength());
			mv.addObject("total", newsService.count(news));
			mv.addObject("isSelected", true);
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询文章异常", e);
		}
		return mv;
	}

	/**
	 * 查询新闻列表
	 * 
	 * @param news
	 * @return
	 */
	@RequestMapping("/loadMore")
	@ResponseBody
	public Map<String, Object> loadMore(News news,int pageNo, String title) {
		Map<String, Object> resp = new HashMap<>();
		try {
			news.setStatus("1");// 状态 0：无效，1：有效
			// 设置分页信息
			if (news.getiDisplayLength() == 0) news.setiDisplayLength(5); // 默认长度5
			if (pageNo <= 0) pageNo = 1; // 默认页数1
			news.setPageNo(pageNo);
			news.setiDisplayStart((news.getPageNo() - 1)* news.getiDisplayLength()); // 显示起始索引
			// 当关键字不为null并且不为空时，通过关键字全分类模糊查询
			// 反之，查询当前分页状态下的信息
			List<News> list = null;
			if (news.getKeyword() != null && !news.getKeyword().trim().isEmpty()) {
				list = newsService.queryByLike(news, news.getKeyword());
				resp.put("dataList",list);
				resp.put("iTotalDisplayRecords",list.size());
				resp.put("isSelected", true);
			} else {
				list = newsService.query(news);
				resp.put("dataList", list);
				resp.put("iTotalDisplayRecords",list.size());
				resp.put("isSelected", false);
			}
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询文章异常", e);
		}
		return resp;
	}

	/**
	 * 新闻详细内容页
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/show-{id}.html")
	public ModelAndView queryDetail(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView(super.t + "/news/newsDetail.jsp");
		try {
			News news = newsService.find(id);
			mv.addObject("news", news);
			mv.addObject("newsClass", newsClass());
			// 增加一次查看
			if (news != null) {
				news.setReadTimes(news.getReadTimes() == null ? 1 : news
						.getReadTimes() + 1);
				newsService.update(news);
			}
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "新闻详细内容页异常", e);
		}

		return mv;
	}

	/**
	 * 新闻分类
	 * 
	 * @return
	 */
	private List<ConfValue> newsClass() {
		List<ConfValue> plist = null;
		try {
			plist = confService.queryByType(GlobalConstant.TYPE_NEWS, 0L);// 新闻分类
			for (ConfValue cv : plist) {
				cv.setChildren(confService.findConfValueByPid(cv.getPid()));
			}
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询新闻分类异常", e);
		}
		return plist;
	}

}
